<?php
class project{
    public static function Run(){
        call_user_func("project::".$_SERVER["REQUEST_METHOD"]."_".$_GET["p"]);
    }

    public static function GET_list(){
        if(!file_exists("data/project.json")){
            echo json_encode([
                "res"=>"OK","list"=>[]
            ]);
            exit;
        }
        $data=json_decode(file_get_contents("data/project.json"),true);
        echo json_encode([
            "res"=>"OK","list"=>$data
        ]);
    }

    public static function POST_add(){
        if(!file_exists("data/project.json")){
            file_put_contents("data/project.json","[]");
        }
        $data=json_decode(file_get_contents("data/project.json"),true);
        array_push($data,[
            "id"=>count($data)+1,
            "s_project_name"=>$_POST["s_project_name"],
            "s_project_path"=>$_POST["s_project_path"],
            "s_status"=>"规划",
            "i_drop"=>0
        ]);
        file_put_contents("data/project.json",json_encode($data,JSON_UNESCAPED_UNICODE));
        echo json_encode([
            "res"=>"OK"
        ],JSON_UNESCAPED_UNICODE);
    }

    public static function POST_edit(){
        if(!file_exists("data/project.json")){
            echo json_encode([
                "res"=>"NG","message"=>"无项目更新"
            ]);
        }
        $data=json_decode(file_get_contents("data/project.json"),true);
        // print_r($data);
        $data[$_POST["id"]-1]["s_project_name"]=$_POST["s_project_name"];
        $data[$_POST["id"]-1]["s_project_path"]=$_POST["s_project_path"];
        file_put_contents("data/project.json",json_encode($data,JSON_UNESCAPED_UNICODE));
        echo json_encode([
            "res"=>"OK"
        ],JSON_UNESCAPED_UNICODE);
    }

    public static function POST_delete(){
        if(!file_exists("data/project.json")){
            echo json_encode([
                "res"=>"NG","message"=>"无项目更新"
            ]);
        }
        $data=json_decode(file_get_contents("data/project.json"),true);
        // print_r($data);
        $data[$_POST["id"]-1]["i_drop"]=1;
        file_put_contents("data/project.json",json_encode($data,JSON_UNESCAPED_UNICODE));
        echo json_encode([
            "res"=>"OK"
        ],JSON_UNESCAPED_UNICODE);
    }
}

project::Run();
?>